package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.sql.SQLException;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JLabel;
import javax.swing.SwingConstants;


public class MainMenu extends JFrame {
	
	private Properties langFile;
	private static String user;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainMenu frame = new MainMenu(null,null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * Create the frame.
	 */
	public MainMenu(Properties prop, String userName) {
		getContentPane().setBackground(SystemColor.menuText);
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		this.setUndecorated(true);
		user = userName;
		if(prop == null)
		{
			langFile = new Properties();
			try{
				FileInputStream in = new FileInputStream("C:\\Documents and Settings\\sneha\\workspace\\examSys\\src\\gui\\lang_srb.properties");
				langFile.load(in);
			}
			catch(Exception e){
				System.out.println("file not found");
				System.exit(0);
			}
		}
		else
		{
			langFile = prop;
		}
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBackground(SystemColor.textInactiveText);
		setJMenuBar(menuBar);
		
		JMenu fileMenu = new JMenu(langFile.getProperty("usr.menu.file"));
		fileMenu.setBackground(SystemColor.textInactiveText);
		menuBar.add(fileMenu);
		JMenuItem newExm = new JMenuItem(langFile.getProperty("usr.menu.file.newExam"));
		newExm.setBackground(SystemColor.controlLtHighlight);
		newExm.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new Exam(langFile).setVisible(true);
			}
		});
		
		fileMenu.add(newExm);
		JMenuItem mntmAddTopicsAnd = new JMenuItem(langFile.getProperty("usr.menu.file.addTopics"));
		fileMenu.add(mntmAddTopicsAnd);
		mntmAddTopicsAnd.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					new InsertTopicSubtopic(langFile).setVisible(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});
		JMenuItem mntmAddQuestion = new JMenuItem(langFile.getProperty("usr.menu.file.addQuesiton"));
		mntmAddQuestion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					new QuestionAdd(langFile).setVisible(true);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		fileMenu.add(mntmAddQuestion);
		fileMenu.add( new JSeparator());
		JMenuItem exitBtn = new JMenuItem(langFile.getProperty("usr.menu.file.Exit"));
		exitBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
				
			}
		});
		
		
		
		fileMenu.add(exitBtn);
		
		JMenu viewBtn = new JMenu(langFile.getProperty("usr.menu.view"));
		viewBtn.setBackground(SystemColor.textInactiveText);
		menuBar.add(viewBtn);
		
		JMenuItem topicsBtn = new JMenuItem(langFile.getProperty("usr.menu.view.topicsSubTopics"));
		viewBtn.add(topicsBtn);
		topicsBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					new ViewTopics(langFile).setVisible(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				
			}
		});
		
		JMenuItem question = new JMenuItem(langFile.getProperty("usr.menu.view.question"));
		viewBtn.add(question);
		viewBtn.add( new JSeparator());
		JMenuItem adminsView = new JMenuItem(langFile.getProperty("usr.menu.view.admins"));
		viewBtn.add(adminsView);
		adminsView.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new Admins(langFile).setVisible(true);
				
			}
		});
		
		JMenu mnUpdate = new JMenu(langFile.getProperty("usr.menu.update"));
		mnUpdate.setBackground(SystemColor.textInactiveText);
		menuBar.add(mnUpdate);
		
		JMenuItem quesUpdate = new JMenuItem(langFile.getProperty("usr.menu.update.question"));
		mnUpdate.add(quesUpdate);
		
		JMenuItem usrDetails = new JMenuItem(langFile.getProperty("usr.menu.update.details"));
		mnUpdate.add(usrDetails);
		
		JMenuItem mntmUpdateExamDetails = new JMenuItem(langFile.getProperty("usr.menu.update.exam"));
		mnUpdate.add(mntmUpdateExamDetails);
		usrDetails.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new UpdateDetails(langFile,user).setVisible(true);
			}
		});
		JMenu mnChangeLanguage = new JMenu(langFile.getProperty("usr.menu.changeLang"));
		mnChangeLanguage.setBackground(SystemColor.textInactiveText);
		menuBar.add(mnChangeLanguage);
		
		JMenuItem mntmSerpski = new JMenuItem(langFile.getProperty("usr.menu.changeLang.serpski"));
		mntmSerpski.setIcon(new ImageIcon(MainMenu.class.getResource("/Images/srbFlag.gif")));
		mnChangeLanguage.add(mntmSerpski);
		mntmSerpski.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				languageChanged(false);
			}
		});
		
		mnChangeLanguage.add( new JSeparator());
		JMenuItem mntmEnglish = new JMenuItem(langFile.getProperty("usr.menu.changeLang.english"));
		mntmEnglish.setIcon(new ImageIcon(MainMenu.class.getResource("/Images/flag_icon_uk.gif")));
		mnChangeLanguage.add(mntmEnglish);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBackground(SystemColor.menuText);
		lblNewLabel.setIcon(new ImageIcon(MainMenu.class.getResource("/Images/logo.jpg")));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		getContentPane().add(lblNewLabel, BorderLayout.CENTER);
		mntmEnglish.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				languageChanged(true);
			}
	});

}
	private void languageChanged(boolean flag){ // true - english , flase - serbian
		
		if(flag)
		{
			try{
				Properties prop = new Properties();
				FileInputStream in = new FileInputStream("C:\\Documents and Settings\\sneha\\workspace\\examSys\\src\\gui\\lang.properties");
				prop.load(in);
				new MainMenu(prop,user).setVisible(true);
				this.setVisible(false);
			}
			catch(Exception e){
				System.out.println("file not found");
			}
		}
		else
		{
			try{
				Properties prop = new Properties();
				FileInputStream in = new FileInputStream("C:\\Documents and Settings\\sneha\\workspace\\examSys\\src\\gui\\lang_srb.properties");
				prop.load(in);
				new MainMenu(prop,user).setVisible(true);
				this.setVisible(false);
			}
			catch(Exception e){
				System.out.println("file not found");
			}
		}
	}
	
}
	
